package cn.itcast.controler;

import cn.itcast.CheckItemService;
import cn.itcast.constant.MessageConstant;
import cn.itcast.pojo.*;
import com.alibaba.dubbo.config.annotation.Reference;
import com.qiniu.streaming.model.ActivityRecords;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkItem")
public class CheckItewController {
    @Reference
    private CheckItemService checkItemService;

    @RequestMapping("/add")
    public Result add(@RequestBody CheckItem checkItem) {
        try {
            checkItemService.add(checkItem);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);


    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

        PageResult pageResult = checkItemService.findPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
        return pageResult;


    }

    @RequestMapping("/delete")
    public Result delete(@RequestParam("id") Integer id) {
        try {
            checkItemService.delete(id);

        } catch (RuntimeException e) {
            return new Result(false, e.getMessage());

        } catch (Exception e) {
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem){
        try {
            checkItemService.edit(checkItem);
        }catch (Exception e){
            return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);


    }
    @RequestMapping("/findById")
    public  Result findById(@RequestParam("id") long id){
         CheckItem item;
        try {
        item = checkItemService.findById(id);
        } catch (Exception e) {
            return new Result(false,"回显数据失败");
        }
return new Result(true,"回显数据成功",item);

    }
    @RequestMapping("/findAll")
    public Result findAll() {

        List<CheckItem> checkGroupList = checkItemService.findAll();
        if (checkGroupList != null && checkGroupList.size() > 0) {
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroupList);


        }
        return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);

    }


}
